package cn.com.wdjedu.androidapp.wjeduandroid.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import butterknife.ButterKnife;
import cn.com.wdjedu.androidapp.wjeduandroid.support.lib.mvputils.BasePresenter;
import cn.com.wdjedu.androidapp.wjeduandroid.ui.baseui.AbstractAppFragment;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by samson on 26/11/2016.
 */

public abstract class BaseFragment<P extends BasePresenter>  extends AbstractAppFragment {
    public Context mContext;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          mContext = getActivity();
     //   mContext = getActivity().getApplication();
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    public void toastShow(int resId) {
        Toast.makeText(mContext, resId, Toast.LENGTH_SHORT).show();
    }

    public void toastShow(String resId) {
        Toast.makeText(mContext, resId, Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        onUnsubscribe();
    }

    private CompositeSubscription mCompositeSubscription;

    public void onUnsubscribe() {
        //取消注册，以避免内存泄露
        if (mCompositeSubscription != null) {
            mCompositeSubscription.unsubscribe();
        }
    }

    public void addSubscription(Subscription subscription) {
//        if (mCompositeSubscription == null) {
        mCompositeSubscription = new CompositeSubscription();
//        }
        mCompositeSubscription.add(subscription);
    }



}